package com.dream.keigezhushou.Activity.Fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.dream.keigezhushou.Activity.Uiutils.JsonParse;
import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.acty.play.GameInfoActivity;
import com.dream.keigezhushou.Activity.acty.play.HotVideoActivity;
import com.dream.keigezhushou.Activity.acty.play.PublishGameActivity;
import com.dream.keigezhushou.Activity.acty.play.PublishVideoActivity;
import com.dream.keigezhushou.Activity.acty.play.RecommendGameActivity;
import com.dream.keigezhushou.Activity.acty.play.VideoInfoActivity;
import com.dream.keigezhushou.Activity.adapter.GameAdapter;
import com.dream.keigezhushou.Activity.adapter.VideoAdapter;
import com.dream.keigezhushou.Activity.bean.GameListBean;
import com.dream.keigezhushou.Activity.bean.Image;
import com.dream.keigezhushou.Activity.bean.Video;
import com.dream.keigezhushou.Activity.url.NetURL;
import com.dream.keigezhushou.Activity.view.NestedScrollViewBottom;
import com.dream.keigezhushou.R;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * 城会玩
 */
public class PlayFragment extends Fragment {


    @BindView(R.id.rl_hot_video)
    RelativeLayout rlHotVideo;
    @BindView(R.id.iv_add_video)
    ImageView ivAddVideo;
    @BindView(R.id.lv_video)
    RecyclerView lvVideo;
    @BindView(R.id.rl_recommend_video)
    RelativeLayout rlRecommendVideo;
    @BindView(R.id.lv_recommend_video)
    RecyclerView lvRecommendVideo;
    @BindView(R.id.scroll_bottom)
    NestedScrollViewBottom scrollBottom;

    private VideoAdapter adapter;
    private Intent intent;


    private List<List<Image>> imagesList;

    private String[][] images = new String[][]{
            {"file:///android_asset/img2.jpg", "250", "250"}
            , {"file:///android_asset/img3.jpg", "250", "250"}
            , {"file:///android_asset/img4.jpg", "250", "250"}
            , {"file:///android_asset/img5.jpg", "250", "250"}
            , {"file:///android_asset/img6.jpg", "250", "250"}
            , {"file:///android_asset/img7.jpg", "250", "250"}
            , {"file:///android_asset/img8.jpg", "250", "250"}
    };
    private GameAdapter gameAdapter;
    private List<Video> videoList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = UiUtils.inflateView(R.layout.play_fragment);
        ButterKnife.bind(this, view);


        imagesList = new ArrayList<>();
//        ArrayList<Image> singleList=new ArrayList<>();
        //从一到7生成7条朋友圈内容，分别是1~7张图片
        for (int i = 0; i < 7; i++) {
            ArrayList<Image> itemList = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                itemList.add(new Image(images[j][0], Integer.parseInt(images[j][1]), Integer.parseInt(images[j][2])));
            }
            imagesList.add(itemList);
        }

        videoList = new ArrayList<Video>();

        initView();
        initData();
        return view;

    }

    //初始化界面
    protected void initView() {

        Log.i("121", "史哥史哥你真棒");

        //获取视频

        OkHttpUtils.post()
                .url(NetURL.getVideo)
                .addParams("page", "1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {

                        Log.i("122", "史哥史哥你真棒");

                        ArrayList<Video> arrayList = JsonParse.jsonToArrayList(response, Video.class);

                        lvVideo.setLayoutManager(new LinearLayoutManager(getActivity()) {
                            @Override
                            public boolean canScrollVertically() {
                                return false;
                            }
                        });
                        adapter = new VideoAdapter(getActivity(), arrayList);
                        lvVideo.setFocusable(false);
                        lvVideo.setAdapter(adapter);

                        adapter.setOnItemClickListener(new MyItemClickListener() {
                            @Override
                            public void onEvaluateClick(int position) {

                                intent = new Intent(getActivity(), VideoInfoActivity.class);
                                startActivity(intent);
                            }

                            @Override
                            public void onShareClick(int position) {

                            }
                        });
                    }
                });


        Log.i("120", "史哥史哥你真棒");
        //获取玩法
        OkHttpUtils.post()
                .url(NetURL.getGame)
                .addParams("page", "1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {

                        Log.i("123", "史哥史哥你真棒");

                        //网络请求成功
                        ArrayList<GameListBean> gameList = JsonParse.jsonToArrayList(response, GameListBean.class);

                        Log.i("000",gameList.toString());
                        lvRecommendVideo.setLayoutManager(new LinearLayoutManager(getActivity()) {

                            @Override
                            public boolean canScrollVertically() {
                                return false;
                            }
                        });
                        gameAdapter = new GameAdapter(getActivity(), gameList);
                        lvRecommendVideo.setFocusable(false);
                        lvRecommendVideo.setVerticalScrollBarEnabled(false);

                        lvRecommendVideo.setAdapter(gameAdapter);

                        gameAdapter.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onEvaluateClick(int position) {

                                intent = new Intent(getActivity(), GameInfoActivity.class);
                                startActivity(intent);
                            }

                            @Override
                            public void onShareClick(int position) {

                            }
                        });

                    }
                });


    }


    //初始化数据
    protected void initData() {

        scrollBottom.setScrollBottomListener(new NestedScrollViewBottom.ScrollBottomListener() {
            @Override
            public void scrollBottom() {

            }
        });


        lvVideo.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {

            }
        });

        //热门视频
        rlHotVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                intent = new Intent(getActivity(), HotVideoActivity.class);
                startActivity(intent);
            }
        });


        //推荐玩法
        rlRecommendVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                intent = new Intent(getActivity(), RecommendGameActivity.class);
                startActivity(intent);
            }
        });


        ivAddVideo.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {

//                Dialog dialog = new Dialog(getActivity());
//                dialog.setContentView(R.layout.layout_dialog); //设置dialog的布局
//
//                Window window = dialog.getWindow();
//                window.setGravity(Gravity.CENTER); //设置dialog的位置
//
//                /**
//                 *
//                 * 设置dialog的大小
//                 */
//
//                WindowManager manager = getActivity().getWindowManager();
//
//                Display display = manager.getDefaultDisplay(); //获取屏幕宽高的工具
//                WindowManager.LayoutParams params = window.getAttributes(); //获取对话框当前的参数值
//                params.width = (int)(display.getWidth()* 0.6); //设置dialog的宽度
//                window.setAttributes(params);
//                dialog.show();  //dialog展示

                LayoutInflater mLayoutInflater = LayoutInflater.from(getActivity());
                LinearLayout mDialog = (LinearLayout) mLayoutInflater.inflate(R.layout.layout_dialog, null);

                final Dialog dialog = new AlertDialog.Builder(getActivity()).create();
                dialog.show();

                dialog.setContentView(mDialog);
                Window window = dialog.getWindow();
                window.setGravity(Gravity.CENTER);

                WindowManager manager = getActivity().getWindowManager();

                Display display = manager.getDefaultDisplay(); //获取屏幕宽高的工具
                WindowManager.LayoutParams params = window.getAttributes(); //获取对话框当前的参数值
                params.width = (int) (display.getWidth() * 0.8); //设置dialog的宽度
                window.setAttributes(params);

                //dialog的点击事件
                mDialog.findViewById(R.id.tv_publish_game).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        intent = new Intent(getActivity(), PublishGameActivity.class);
                        startActivity(intent);

                        dialog.dismiss(); //关闭dialog
                    }
                });

                mDialog.findViewById(R.id.tv_publish_video).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        intent = new Intent(getActivity(), PublishVideoActivity.class);
                        startActivity(intent);
                        dialog.dismiss(); //关闭dialog
                    }
                });
            }
        });

    }

//    @Override
//    public void onBackPressed() {
//        if (JCVideoPlayer.backPress()) {
//            return;
//        }
//        super.onBackPressed();
//    }

    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                finish();
                onDestroy();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    //定义视频recyclerview的item点击接口

    public interface MyItemClickListener {

        public void onEvaluateClick(int position);

        public void onShareClick(int position);
    }

    //定义玩法的item的点击接口

    public interface OnItemClickListener {

        public void onEvaluateClick(int position);

        public void onShareClick(int position);
    }
}
